package com.league.zjxn.main.access

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.GsonUtils
import com.league.zjxn.R
import com.league.zjxn.jetpack.vmodel.AccessViewModel
import com.league.zjxn.main.MainActivity
import com.league.zjxn.main.popup.LoadingDialog
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerAbstractActivity
import com.module.lemlin.owner.OwnerViewModeActivity

class AccessActivity : OwnerViewModeActivity<AccessViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(AccessActivity::class.java)
        }
    }

    override fun viewModel(): AccessViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_access

    override fun initView() {
        supportFragmentManager(LoginPassFragment.newInstance(), (false))
    }

    override fun initViewMode() {
        viewModel.userInfoLiveData.observe(this as LifecycleOwner, { response ->
            responseLoadingDialog(response)
            if (response.status != Status.SUCCESS)
                return@observe
            MainActivity.open()
            finish()
        })
        viewModel.sendSmsCodeLiveData.observe(this as LifecycleOwner, { response ->
            responseLoadingDialog(response)
        })
        viewModel.userRegisterLiveData.observe(this as LifecycleOwner, { response ->
            responseLoadingDialog(response)
        })
        viewModel.userUpdatePassLiveData.observe(this as LifecycleOwner, { response ->
            responseLoadingDialog(response)
            if (response.status != Status.SUCCESS)
                return@observe
            toast(response.data?.msg)
            onClickBackPressed()
            onClickBackPressed()
        })
    }

    private fun responseLoadingDialog(response: HttpResponseBody<*>) {
        when (response.status) {
            Status.START -> LoadingDialog.show(
                this as Context, resId = R.layout.dialog_load_style_1
            )
            Status.FAILURE -> {
                LoadingDialog.dismiss()
                toast(response.error?.msg)
            }
            Status.SUCCESS -> {
                LoadingDialog.dismiss()
            }
        }
    }

    /**
     * 发送验证码
     * */
    fun onClickSendSms(view: View? = null) {
        if (view == null)
            return
        if (view.id == R.id.tvLoginCodeSend) {
            viewModel.userSendSmsCodeLogin(view as TextView)
        } else if (view.id == R.id.tvRegisterSend) {
            viewModel.userSendSmsCodeRegister(view as TextView)
        } else if (view.id == R.id.tvUpdateCodeSend) {
            viewModel.userSendSmsCodeForget(view as TextView)
        }
    }

    /**
     * 密码登录提交
     * */
    fun onClickLoginPassSubmit(view: View? = null) {
        viewModel.userLoginPass()
    }

    /**
     * 验证码登录提交
     * */
    fun onClickLoginCodeSubmit(view: View? = null) {
        viewModel.userLoginSms()
    }

    /**
     * 注册账号提交
     * */
    fun onClickRegisterSubmit(view: View? = null) {
        viewModel.userRegister()
    }

    /**
     * 账号核验提交
     * */
    fun onClickUpdateCodeSubmit(view: View? = null) {
        supportFragmentManager(UpdatePassFragment.newInstance())
    }

    /**
     * 设置新密码提交
     * */
    fun onClickUpdatePassSubmit(view: View? = null) {
        viewModel.userUpdatePass()
    }

    /**
     * 去注册页面
     * */
    fun onClickGoRegister(view: View? = null) {
        supportFragmentManager(RegisterFragment.newInstance())
    }

    /**
     * 去短信登录页面
     * */
    fun onClickGoLoginCode(view: View? = null) {
        supportFragmentManager(LoginCodeFragment.newInstance())
    }

    /**
     * 去核验账号页面
     * */
    fun onClickGoUpdateCode(view: View? = null) {
        supportFragmentManager(UpdateCodeFragment.newInstance())
    }

    /**
     * 去设置密码页面
     * */
    fun onClickGoUpdatePass(view: View? = null) {
        supportFragmentManager(UpdatePassFragment.newInstance())
    }

    /**
     * 返回上一页
     * */
    fun onClickBackPressed(view: View? = null) {
        onBackPressed()
    }

    private fun supportFragmentManager(fragment: Fragment, isToBackStack: Boolean = true) {
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        if (isToBackStack) {
            transaction.setCustomAnimations(
                R.animator.slide_right_in, R.animator.slide_left_out,
                R.animator.slide_left_in, R.animator.slide_right_out
            )
            transaction.replace(R.id.flAccessBody, fragment)
            transaction.addToBackStack(null)
        } else {
            transaction.add(R.id.flAccessBody, fragment)
        }
        transaction.commit()
    }
}